﻿<?php
require_once('../../config.php');
 require_once($CFG->libdir . '/adminlib.php');
 require_once($CFG->dirroot . '/local/myplugin/lib.php');
require_once($CFG->dirroot . "/local/myplugin/forms.php");


global $DB;
$option=array();

/* Ket noi den file javascript - module.js */
$PAGE->requires->js_init_call('M.local_myplugin');	
$cssdir =  $CFG->wwwroot.'/local/myplugin/mycss.css';
echo '<link rel="stylesheet" href="'.$cssdir.'" type="text/css" media="screen" />';
//echo '<link rel="stylesheet" href="style.css" type="text/css" media="screen" />';

/* Chuan bi trang web can hien thi */	
$url = new moodle_url($CFG->wwwroot.'local/myplugin/select_block_of_knowledge.php');
$PAGE->set_url($url);
$PAGE->set_title('Choice Block of Knowledge');
$PAGE->set_heading('Choice Block of Knowledge');
$PAGE->set_pagelayout('standard');

//Them thanh menu ngang ung voi trang trong duong link
navigation_node::override_active_url(new moodle_url($CFG->wwwroot.'/local/myplugin/select_block_of_knowledge.php', array('param'=>'value')));

echo $OUTPUT->header();

/* Viet doan code cua ban o day */
//add tab vào trang
$mylib = new curriculum_management();
//set tab hien tai 
$mylib ->view_tabs('addblock');

$choose_block_knowledge_form = new form_choose_block_knowledge();
//bat su kien nhan nu cancel
if ($choose_block_knowledge_form->is_cancelled()) {

echo 'hủy thao tác';

}

//kiem tra neu dc submitted thì làm gì
if ($fromform = $choose_block_knowledge_form->get_data()) 
{
	//lay program id va major id tu combobox
	//vì 2 ku nay ghep voi nhau bang dau + nen phai cat chuoi de lay major id,  dang ghep chuoi program_id+major_id
	$pro_maj_id= $fromform->cbprogram;
	//ham cat chuoi
	$id_array= explode("+", $pro_maj_id);
	$major_id=$id_array[1];
	$program_id=$id_array[0];
	$block_id= $fromform->cbblock;
	$desc= $fromform->txtdesc;
	$option['program_id']=$program_id;
	$option['major_id']=$major_id;
	$option['bok_id']=$block_id;
	$option['blockofknowledge_id']=$block_id;
	$option['bok_desc']=$desc;
	//kiem tra da co desc cho block nay chua, neu chua có thì add, neu có thì update
	$sql_get_block="select * from mdl_program_block_desc where program_id='".$program_id."' and major_id='".$major_id."' and bok_id='".$block_id."'";
	if ($foundid = $DB->get_records_sql($sql_get_block))
	{
		//có desc rồi nen chi update du lieu
		foreach($foundid as $item)
		{
			$option['id']=$item->id;
			$mylib->update_pro_bok_desc($option);
		}
	}
	else	
	{
		// chua có desc add new
		$mylib->add_pro_bok_desc($option);
	}
	//============add subject =========================
	$cbchon=$_POST['cbchon'];
	foreach($cbchon as $item)
	{
		$option['subject_id']=$item;
		$sql_check_subject="SELECT subject_id FROM mdl_subject_major WHERE program_id='".$program_id."' and major_id='".$major_id."' and blockofknowledge_id='".$block_id."' and subject_id='".$item."'";
		if (!$foundid = $DB->get_records_sql($sql_check_subject))
		{
			$mylib->add_subject_major($option);
		}
	}
}

$choose_block_knowledge_form->display();
/*------Ket thuc doan code cua ban-------*/

echo $OUTPUT->footer();

